package com.cskaoyan.servlet;


import com.cskaoyan.bean.IssueListData;
import com.cskaoyan.bean.IssueParameter;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.model.MarketIssue;
import com.cskaoyan.service.IssueService;
import com.cskaoyan.service.impl.IssueServiceImpl;

import com.cskaoyan.utils.JacksonUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/admin/issue/*")
public class AdminIssueServlet extends CommonServlet {
    IssueService issueService = new IssueServiceImpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response){
        //get请求得到各项参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //Issue自己的Parameter，继承于Parameter
        IssueParameter issueParameter = new IssueParameter();
        try {
            //复制到IssueParameter
            BeanUtils.copyProperties(issueParameter,parameterMap);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        //得到Mapper查询到的IsseListData数据
        IssueListData issueListData = issueService.getIssue(issueParameter);

        return BaseRespVo.ok(issueListData);
    }

    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String s = request.getReader().readLine();
        //将传来的Json数据转为键值对对象
        Map<String,String> map = JacksonUtil.read(s, Map.class);


        //将传来的对象数据进行插入
        MarketIssue marketIssue = issueService.insertNewIssue(map);
        return BaseRespVo.ok(marketIssue);
    }

    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String s = request.getReader().readLine();
        Map<String,String> map = JacksonUtil.read(s, Map.class);

        //将传来的数据进行删除
        issueService.deleteIssue(map);

        return BaseRespVo.ok(null);
    }

    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String s = request.getReader().readLine();
        Map<String,String> map = JacksonUtil.read(s, Map.class);


        //更新传来的数据
        MarketIssue marketIssue = issueService.updateIssue(map);

        return BaseRespVo.ok(marketIssue);
    }




}
